package com.you.modules.user.controller;

import com.you.common.model.Response;
import com.you.common.util.ResponseUtil;
import com.you.modules.user.model.UserInfo;
import com.you.modules.user.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 *<p>
 * 用户相关控制层
 *</p>
 *
 * @author : liuyang
 * @date : 2018-05-12 星期六 02:08:55
 * @since : 1.0.0
 */

@Controller
@RequestMapping("/web/user/userinfo")
@Api(value = "user-api", description = "有关于用户的CURD操作")
public class UserInfoController {
    private final static Logger LOG = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private IUserInfoService userInfoService;


    @Autowired
    private HttpServletRequest httpServletRequest;

    @ApiOperation(value = "查看用户列表",notes = "show user list",httpMethod = "GET")
    @GetMapping(value = "/list")
    @ResponseBody
    public Response listUsers() {
        LOG.info("listUsers");
        return ResponseUtil.success(userInfoService.findAll());
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public Response addUser(@RequestBody UserInfo userInfo) {
        return ResponseUtil.success(userInfoService.save(userInfo));
    }

    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public Response getUser(@PathVariable("id") Integer id) {
        UserInfo userInfo = userInfoService.get(id);
        return ResponseUtil.success(userInfo);
    }

    @GetMapping(value = "/find")
    @ResponseBody
    public Response findUser(@RequestParam("keyword") String username) {
        return ResponseUtil.success(userInfoService.findByUsername(username));
    }

    @GetMapping(value = "/session/id/get")
    @ResponseBody
    public Response getSessionId() {
        return ResponseUtil.success(httpServletRequest.getSession().getId());
    }

}
